gravity与layout_gravity的区别

gravity与layout_gravity的区别gravity与layout_gravity的区别

大家好,又见面了,我是你们的朋友全栈君。

含义

gravity是相对于元素本身而言的,用于控制布局中控件的对齐方式,如果没有子控件,则表示内容的对齐方式。gravity常用值有top、bottom、left、right、center等,可以使用”|”将多个值连接起来。

layout_gravity是相对于父布局容器而言的,用于设置在父容器中的对齐方式。

例子

在LinearLayout中放置一个TextView,通过配置gravity和layout_gravity观察TextView出现的位置。
xml文件代码:在TextView中添加属性,android:layout_gravity=”center”

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"//设置TextView在父控件LinearLayout中居中
        android:text="@string/hello_world" />

</LinearLayout>
在LinearLayout中修改代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center">//设置子控件TextView居中
    <TextView
        android:id="@+id/view1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:layout_gravity="center"
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别=
gravity与layout_gravity的区别


从上面的运行结果就可以看出,android:layout_gravity=”center”是让TextView显示在Layout的中间。
android:gravity=”center”则让LinearLayout中的子控件TextView居中。
再看一个例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center_horizontal"//添加代码的位置
        android:layout_gravity="center
        android:text="@string/hello_world" />

</LinearLayout>

gravity与layout_gravity的区别


TextView中没有子控件,那么它会对TextView中的内容起作用。字体在TextView中居中显示。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/159825.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • B. Bell Ringing

    B. Bell Ringing题目链接https://www.jisuanke.com/contest/1410题目要求只有两点,要求1:要把n个数的全排列打印出来,要求2:相邻的两层数满足同一个数字的位置变化之多一个单位比如说123与132这样是符合题意的但是123与312是不行的因为3的位置变了两个单位同理123与231也不符合题意。这是一到递归题,n个数的全排列可以有n-1个…

    2025年8月12日
    4
  • 嵌入式笔试面试题目系列(汇总)「建议收藏」

    嵌入式笔试面试题目系列(汇总)「建议收藏」本系列按类别对题目进行分类整理,这样有利于大家对嵌入式的笔试面试考察框架有一个完整的理解。

    2025年7月23日
    2
  • Java 编码问题

    Java 编码问题

    2021年10月7日
    37
  • QueryInterface详解 COM

    QueryInterface详解 COMQueryInterface接口查询IUnknown:      所有的COM接口均需要继承IUnknown接口。因此,若某个用户拥有一个IUnknown接口指针,它并不需要知道它所拥有的接口指针到底是什么类型的,而只需要通过此接口就可以用来查询其他接口就行了。      由于所有的COM接口都继承了IUnknown,每个接口的vbtl的前三项都是QueryInterface,A

    2022年6月29日
    29
  • Window安装Redis并设置为开机启动

    Window安装Redis并设置为开机启动

    2021年11月7日
    40
  • 目标检测的目的_小目标检测问题

    目标检测的目的_小目标检测问题我们在评价一个目标检测算法的“好坏”程度的时候,往往采用的是pascalvoc2012的评价标准mAP。网上一些资料博客参差不齐,缺乏直观易懂的正确说明。希望这篇博文能够给大家一点帮助。mAP历史目标检测的mAP计算方式在2010年的voc上发生过变化,目前基本都是采用新的mAP评价标准。(我有个小疑问就是明明是2010年修改的,但是貌似现在大家都称这种计算方式为2012)所…

    2022年10月12日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号